package com.tju.backend.utils.tool;

import javax.servlet.http.HttpServletRequest;

/**
 * 常用工具
 *
 * @USER: CLS
 */
public class GeneralUtils {

    /**
     * 获取客户端ip地址
     *
     * @param request
     * @return
     */
    public static String getCliectIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.trim().equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.trim().equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.trim().equals("") || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        // 多个路由时，取第一个非unknown的ip
        final String[] arr = ip.split(",");
        for (final String str : arr) {
            if (!"unknown".equalsIgnoreCase(str)) {
                ip = str;
                break;
            }
        }
        return ip;
    }

    /**
     * 计算出当前值和上次的值还差多少
     *
     * @param current  当前值
     * @param lastTime 上次值
     * @return
     */
    public static boolean weightByScope(int current, int lastTime) {
        int sum = (Math.max(current, lastTime)) - (Math.min(current, lastTime));
        return sum <= 10;
    }
}
